import xml.dom.minidom
from xml.xpath.Context import Context
from xml.xpath import Evaluate
from xml.xpath.NamespaceNode import NamespaceNode


def parse_float(value):
    if isinstance(value, unicode):
            rtn = float(value.replace(',',''))
    else:
            rtn = float(value)
    return rtn

def cnvtClean(e):
    return [x for x in e if str(x.__class__) != 'xml.dom.minidom.Text' ]


def parse_stock(dom_child):
    stockid = dom_child.getAttribute('id')    
    childs = cnvtClean( dom_child.childNodes )
    for child in childs:
        if child.nodeName == "Price":
               price = child.childNodes[0].data
        elif child.nodeName == "Buzz":
               buzz = child.childNodes[0].data
        elif child.nodeName == "Shares":
               shares = child.childNodes[0].data
        else:
            print "---unknown xml element", child.nodeName, "in", s
    return (stockid, price, buzz, shares)

def parse_allinfo(content,timestamp):
    print "--- got allinfo.xml | Lenght: ", len(content)

    inputDom = xml.dom.minidom.parseString(content)
    c = Context(inputDom.documentElement)

    xpathstr='/ResultSet/Market'
    result = Evaluate(xpathstr, context=c)

    for d in result:
        childs = cnvtClean( d.childNodes )
        stocks= []
        for child in childs:
            if child.nodeName == "Stock":
                stocks.append(parse_stock(child))
            elif child.nodeName == "Shares":
                mkt_shares = child.childNodes[0].data
            elif child.nodeName == "MktCap":
                mkt_cap = parse_float(child.childNodes[0].data)
            else:
                print "--- unknown xml element", child.nodeName, "in", m
        for s in stocks:
            (stockid, price, buzz, shares) = s
            print "INSERT INTO stock_history VALUES(%s, %s, %s, %s, %s, %s, %.2f);"  % (stockid, timestamp,price, buzz, shares, mkt_shares, mkt_cap)


if __name__ == '__main__':
    import os, time
    directory = '/home/ncesar/lab/stock_reader/archive'
    archives = [x for x in os.listdir(directory) if x[-11:] == 'allinfo.xml']

    for a in archives:
        timestamp = time.mktime(time.strptime(a[0:16],'%Y-%m-%d-%H-%M'))
        parse_allinfo("\n".join(file(directory+'/'+a, "r").readlines()),timestamp)
